/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sisgraf.dao;
import sisgraf.model.Funcao;
import sisgraf.util.HibernateUtil;
import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
 *
 * @author Jordan de Carvalho Guerra
 */
public class FuncaoDaoImp implements FuncaoDao {
    private Session sessionLock;
    private Transaction transacaoLock;
    public boolean save(Funcao funcao) {
        boolean retorno = true;
        Session session = null;
        Transaction t=null;
        try
        {
            session = HibernateUtil.getSessionFactory().openSession();
            t = session.beginTransaction();
            session.save(funcao);
            t.commit();
        } catch(Exception e)
        {
            retorno=false;
            if (t != null){
                t.rollback();
            }
        } finally
        {
            if (session != null){
                session.close();
            }
            return retorno;
        }
    }
    public boolean update(Funcao funcao) {
        boolean retorno = true;
        Session session = null;
        Transaction t=null;
        try
        {
            session = HibernateUtil.getSessionFactory().openSession();
            t = session.beginTransaction();
            session.update(funcao);
            t.commit();
        } catch(Exception e)
        {
            retorno=false;
            if (t != null){
                t.rollback();
            }
        } finally
        {
            if (session != null){
                session.close();
            }
            return retorno;
        }
    }
    public Funcao getFuncao(int id) {
        Funcao funcao=null;
        Session session = null;
        Transaction t=null;
        try
        {
            session = HibernateUtil.getSessionFactory().openSession();
            t = session.beginTransaction();
            funcao= (Funcao) session.get(Funcao.class, id);
        } catch(Exception e)
        {
            funcao=null;
        } finally
        {
            if (session != null){
                session.close();
            }
            return funcao;
        }
    }
    public Funcao getLockFuncao(int id) {
        Funcao funcao=null;
        try
        {
            this.sessionLock = HibernateUtil.getSessionFactory().openSession();
            this.transacaoLock = this.sessionLock.beginTransaction();
            funcao = (Funcao) this.sessionLock.get(Funcao.class, id, LockMode.UPGRADE);
        } catch(Exception e) {
            funcao=null;
            if (this.transacaoLock != null){
                this.transacaoLock.rollback();
            }
            if (this.sessionLock != null){
                this.sessionLock.close();
            }
        } finally
        {
            return funcao;
        }
    }
    public boolean settLockFuncao(Funcao funcao) {
        boolean retorno=true;
        int id = funcao.getId();
        try
        {
            this.sessionLock.update(funcao);
            this.transacaoLock.commit();
        } catch(Exception e) {
            retorno=false;
            if (this.transacaoLock != null){
                this.transacaoLock.rollback();
            }
            if (this.sessionLock != null){
                this.sessionLock.close();
            }
        } finally
        {
            return retorno;
        }
    }
    public boolean unLockFuncao() {
        boolean retorno=true;
        try
        {
            this.transacaoLock.rollback();
        } catch(Exception e) {
            retorno=false;
            if (this.sessionLock != null){
                this.sessionLock.close();
            }
        } finally
        {
            return retorno;
        }
    }
    public List<Funcao> list(String par) {
        List lista=null;
        Session session = null;
        Transaction t=null;
        try
        {
            session = HibernateUtil.getSessionFactory().openSession();
            t = session.beginTransaction();
            String sql = "from Funcao";
            if (!par.equals("")) sql=sql + " f where "+par;
            lista = session.createQuery(sql).list();
        } catch(Exception e)
        {
            lista=null;
        } finally
        {
            if (session != null){
                session.close();
            }
            return lista;
        }
    }
    public boolean remove(Funcao funcao) {
        boolean retorno = true;
        Session session = null;
        Transaction t=null;
        try
        {
            session = HibernateUtil.getSessionFactory().openSession();
            t = session.beginTransaction();
            session.delete(funcao);
            t.commit();
        } catch(Exception e)
        {
            retorno=false;
            if (t != null){
                t.rollback();
            }
        } finally
        {
            if (session != null){
                session.close();
            }
            return retorno;
        }
    }
}
